SPB Git

spb/prisme Public MIT

Navigateur iOS intelligent — chaque page comprise localement avant d'être affichée. SwiftUI · WebKit · Foundation Models, 100% on-device.

Swift 96.7% JavaScript 3.3%
1.6 KB · 52 lines swift
Raw Blame History
1//2//  ContentRuleManager.swift3//  Prisme4//5//  Author: Simon-Pierre Boucher <contact@spboucher.ai>6//78import Foundation9import Observation10import WebKit1112/// Compiles the bundled blocker rules into a `WKContentRuleList`.13/// Compilation is async and off the rendering critical path: the first page14/// can start loading before rules are ready; the pool applies them to every15/// live web view as soon as compilation finishes.16@MainActor17@Observable18final class ContentRuleManager {19    private static let identifier = "prisme-blocker-v1"2021    private(set) var ruleList: WKContentRuleList?2223    func compileIfNeeded() async {24        guard ruleList == nil else { return }25        guard let store = WKContentRuleListStore.default() else { return }2627        // Reuse a previously compiled list when the bundle hasn't changed.28        if let cached = try? await store.contentRuleList(forIdentifier: Self.identifier) {29            ruleList = cached30            return31        }3233        guard34            let url = Bundle.main.url(forResource: "blockerRules", withExtension: "json"),35            let json = try? String(contentsOf: url, encoding: .utf8)36        else {37            assertionFailure("blockerRules.json missing from bundle")38            return39        }4041        do {42            ruleList = try await store.compileContentRuleList(43                forIdentifier: Self.identifier,44                encodedContentRuleList: json45            )46        } catch {47            // Blocking is an enhancement, never a gate: browse without it.48            ruleList = nil49        }50    }51}52