2021-08-03 16:55:52 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
// Copyright © 2021 Hardcore Engineering Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public 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 https://www.eclipse.org/legal/epl-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.
|
|
|
|
//
|
|
|
|
|
|
|
|
import type { Doc, PropertyType } from './classes'
|
2021-09-22 08:41:03 +00:00
|
|
|
import type { Position } from './tx'
|
2021-08-03 16:55:52 +00:00
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2021-09-26 10:48:15 +00:00
|
|
|
export type _OperatorFunc = (doc: Doc, op: any) => void
|
2021-08-03 16:55:52 +00:00
|
|
|
|
|
|
|
function $push (document: Doc, keyval: Record<string, PropertyType>): void {
|
|
|
|
const doc = document as any
|
|
|
|
for (const key in keyval) {
|
2021-09-22 08:41:03 +00:00
|
|
|
if (doc[key] === undefined) {
|
|
|
|
doc[key] = []
|
|
|
|
}
|
|
|
|
const val = keyval[key]
|
|
|
|
if (typeof val === 'object') {
|
|
|
|
const arr = doc[key] as Array<any>
|
|
|
|
const desc = val as Position<PropertyType>
|
|
|
|
arr.splice(desc.$position, 0, ...desc.$each)
|
2021-08-03 16:55:52 +00:00
|
|
|
} else {
|
2021-09-22 08:41:03 +00:00
|
|
|
doc[key].push(val)
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 08:41:03 +00:00
|
|
|
function $pull (document: Doc, keyval: Record<string, PropertyType>): void {
|
|
|
|
const doc = document as any
|
|
|
|
for (const key in keyval) {
|
|
|
|
const arr = doc[key] as Array<any>
|
|
|
|
doc[key] = arr.filter(val => val !== keyval[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 09:15:09 +00:00
|
|
|
function $pushMixin (document: Doc, options: any): void {
|
|
|
|
const doc = document as any
|
|
|
|
const mixinId = options.$mixin
|
|
|
|
if (mixinId === undefined) { throw new Error('$mixin must be specified for $push_mixin operation') }
|
|
|
|
const mixin = doc[mixinId]
|
|
|
|
const keyval = options.values
|
|
|
|
for (const key in keyval) {
|
|
|
|
const arr = mixin[key]
|
|
|
|
if (arr === undefined) {
|
|
|
|
mixin[key] = [keyval[key]]
|
|
|
|
} else {
|
|
|
|
arr.push(keyval[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 10:48:15 +00:00
|
|
|
function $inc (document: Doc, keyval: Record<string, number>): void {
|
|
|
|
const doc = document as unknown as Record<string, number | undefined>
|
|
|
|
for (const key in keyval) {
|
|
|
|
const cur = doc[key] ?? 0
|
|
|
|
doc[key] = cur + keyval[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
const operators: Record<string, _OperatorFunc> = {
|
2021-08-05 09:15:09 +00:00
|
|
|
$push,
|
2021-09-22 08:41:03 +00:00
|
|
|
$pull,
|
2021-09-26 10:48:15 +00:00
|
|
|
$pushMixin,
|
|
|
|
$inc
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:47:17 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function isOperator (o: Record<string, any>): boolean {
|
|
|
|
if (o === null || typeof o !== 'object') { return false }
|
|
|
|
const keys = Object.keys(o)
|
|
|
|
return keys.length > 0 && keys.every((key) => key.startsWith('$'))
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* @param name -
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export function _getOperator (name: string): _OperatorFunc {
|
2021-08-03 16:55:52 +00:00
|
|
|
const operator = operators[name]
|
|
|
|
if (operator === undefined) throw new Error('unknown operator: ' + name)
|
|
|
|
return operator
|
|
|
|
}
|