MCPcopy
hub / github.com/marktext/marktext / _emitStateChange

Method _emitStateChange

packages/muya/src/state/index.ts:149–182  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

147 }
148
149 private _emitStateChange() {
150 if (this._isGoing)
151 return;
152
153 this._isGoing = true;
154
155 requestAnimationFrame(() => {
156 // Wrap compose in a lambda — `Array.prototype.reduce` passes
157 // (acc, current, index, array) to the callback, but
158 // `json1.type.compose` only accepts (op1, op2). Without the
159 // wrapper TS rejects the signature mismatch.
160 // `compose` returns JSONOp (= null | JSONOpList); when the cache
161 // contains at least one op the result is the composed list,
162 // never null. The reduce above runs only when _operationCache is
163 // non-empty (guarded by the requestAnimationFrame in
164 // `_emitStateChange`), and a non-empty cache always composes to
165 // a non-null op.
166 const op = this._operationCache.reduce(
167 (acc, curr) => json1.type.compose(acc, curr) as JSONOpList,
168 );
169 const prevDoc = this.getState();
170 this.apply(op);
171 // TODO: remove doc in future
172 const doc = this.getState();
173 this.muya.eventCenter.emit('json-change', {
174 op,
175 source: 'user',
176 prevDoc,
177 doc,
178 });
179 this._operationCache = [];
180 this._isGoing = false;
181 });
182 }
183}
184
185export default JSONState;

Callers 4

insertOperationMethod · 0.95
removeOperationMethod · 0.95
editOperationMethod · 0.95
replaceOperationMethod · 0.95

Calls 5

getStateMethod · 0.95
applyMethod · 0.95
composeMethod · 0.80
emitMethod · 0.80
reduceMethod · 0.45

Tested by

no test coverage detected