idlize
1/*
2* Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15
16import { KoalaCallsiteKey, int32 } from "@koalaui/common"17import { __context, __id } from "../internals"18import { memoEntry1, memoEntry2 } from "./entry"19
20/**
21* Sequentially repeats the {@link action}.
22*
23* An {@link action} unit accepts a sequential index as an argument.
24*
25* @param count the number of {@link action}'s to repeat, 0 by default
26* @param action an action to repeat
27*
28* @see RepeatWithKey
29* @see RepeatByArray
30* @see Items
31*
32* @memo
33*/
34export function Repeat(35count: int32,36/** @memo */37action: (index: int32) => void38) {39for (let i = 0; i < count; i++) {40memoEntry1<int32, void>(__context(), i, action, i)41}42}
43
44/**
45* Sequentially repeats the {@link action} using the {@link key} callback.
46*
47* By default, each {@link action} has an implicit key which provides the {@link action}'s
48* result uniqueness. The {@link key} callback can be used to provide a custom key per
49* {@link action}, based on the {@link action}'s index accepted as an argument.
50*
51* @param count the number of {@link action}'s to repeat, 0 by default
52* @param key a callback to provide custom keys
53* @param action an action to repeat
54*
55* @see Repeat
56*
57* @memo
58*/
59export function RepeatWithKey(60count: int32,61key: (index: int32) => KoalaCallsiteKey,62/** @memo */63action: (index: int32) => void64) {65for (let i = 0; i < count; i++) {66memoEntry1<int32, void>(__context(), key(i), action, i)67}68}
69
70/**
71* Sequentially repeats the {@link action} by iterating the {@link array},
72* passing an element of the {@link array} as the {@link action}'s argument.
73*
74* @param array the array to iterate
75* @param key a callback to provide custom keys
76* @param action an action to repeat
77*
78* @see RepeatWithKey
79*
80* @memo
81*/
82export function RepeatByArray<T>(83array: ReadonlyArray<T>,84key: (element: T, index: int32) => KoalaCallsiteKey,85/** @memo */86action: (element: T, index: int32) => void87) {88const length = array.length89for (let i = 0; i < length; i++) {90const e: T = array[i]91memoEntry2<T, int32, void>(__context(), key(e, i), action, e, i)92}93}
94
95/**
96* Sequentially repeats the {@link action} for every element in the given range.
97*
98* @param start - the start index, inclusive
99* @param end - the end index, exclusive
100* @param element - the function to provide an element by its index
101* @param key - the function to provide a key for the element
102* @param action - the memo function to be invoked for every element
103*
104* @memo
105*/
106export function RepeatRange<T>(107start: int32,108end: int32,109element: (index: int32) => T,110key: (element: T, index: int32) => KoalaCallsiteKey,111/** @memo */112action: (element: T, index: int32) => void113) {114for (let i: int32 = start; i < end; i++) {115const e: T = element(i)116memoEntry2<T, int32, void>(__context(), key(e, i), action, e, i)117}118}
119