UNPKG

@react-native-ohos/realm

Version:

Realm by MongoDB is an offline-first mobile database: an alternative to SQLite and key-value stores

91 lines 3.28 kB
"use strict"; //////////////////////////////////////////////////////////////////////////// // // Copyright 2022 Realm Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // //////////////////////////////////////////////////////////////////////////// Object.defineProperty(exports, "__esModule", { value: true }); exports.ObjectListeners = void 0; const binding_1 = require("./binding"); const Listeners_1 = require("./Listeners"); const ClassHelpers_1 = require("./ClassHelpers"); const symbols_1 = require("./symbols"); /** @internal */ class ObjectListeners { realm; object; /** * Storage for the memoized, lazily created object notifier. */ internal; constructor(realm, object) { this.realm = realm; this.object = object; this.properties = (0, ClassHelpers_1.getClassHelpers)(this.object.constructor).properties; } properties; listeners = new Listeners_1.Listeners({ add: (callback, keyPaths) => { const token = this.notifier.addCallback((changes) => { try { callback(this.object, { deleted: changes.isDeleted, changedProperties: changes.changedColumns.map(this.properties.getName), }); } catch (err) { // Scheduling a throw on the event loop, // since throwing synchronously here would result in an abort in the calling C++ setImmediate(() => { throw err; }); } }, keyPaths ? this.mapKeyPaths(keyPaths) : undefined); // Get an actual NotificationToken for the bigint value return binding_1.binding.NotificationToken.forObject(this.notifier, token); }, remove(token) { token.unregister(); }, }); /** * A memoized, lazily created object notifier. */ get notifier() { let notifier = this.internal; if (notifier) { return notifier; } else { notifier = binding_1.binding.Helpers.makeObjectNotifier(this.realm, this.object[symbols_1.OBJECT_INTERNAL]); this.internal = notifier; return notifier; } } addListener(callback, keyPaths) { this.listeners.add(callback, keyPaths); } removeListener(callback) { this.listeners.remove(callback); } removeAllListeners() { this.listeners.removeAll(); } mapKeyPaths(keyPaths) { return this.realm.createKeyPathArray(this.object.objectSchema().name, keyPaths); } } exports.ObjectListeners = ObjectListeners; //# sourceMappingURL=ObjectListeners.js.map