()
| 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 | |
| 185 | export default JSONState; |
no test coverage detected